﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Data.ConnectionUI;

namespace KeelKit.Core
{
    public partial class UserControl1 : UserControl, IDataConnectionUIControl
    {
        public UserControl1()
        {
            this.InitializeComponent();
            this.RightToLeft = RightToLeft.Inherit;
            
           
        }
        public void Initialize(IDataConnectionProperties connectionProperties)
        {
            if (connectionProperties == null)
            {
                throw new ArgumentNullException("connectionProperties");
            }
            if (!(connectionProperties is MySqlConnectionProperties))
            {
                throw new ArgumentException( ("OracleConnectionUIControl_InvalidConnectionProperties"));
            }
            
            this._connectionProperties = connectionProperties;
        }

        public void LoadProperties()
        {
            this._loading = true;
            this.serverTextBox.Text = this.Properties[this.ServerProperty] as string;
            this.userNameTextBox.Text = this.Properties[this.UserNameProperty] as string;
            this.passwordTextBox.Text = this.Properties[this.PasswordProperty] as string;
           
            this._loading = false;
        }

        protected override void OnParentChanged(EventArgs e)
        {
            base.OnParentChanged(e);
            if (base.Parent == null)
            {
                this.OnFontChanged(e);
            }
        }

        protected override void OnRightToLeftChanged(EventArgs e)
        {
            base.OnRightToLeftChanged(e);
            if (((base.ParentForm != null) && base.ParentForm.RightToLeftLayout) && (this.RightToLeft == RightToLeft.Yes))
            {
                LayoutUtils.MirrorControl(this.serverLabel, this.serverTextBox);
            }
            else
            {
                LayoutUtils.UnmirrorControl(this.serverLabel, this.serverTextBox);
            }
        }

        protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
        {
            Size size = base.Size;
            this.MinimumSize = Size.Empty;
            base.ScaleControl(factor, specified);
            this.MinimumSize = new Size((int)Math.Round((double)(size.Width * factor.Width)), (int)Math.Round((double)(size.Height * factor.Height)));
        }

        private void SetPassword(object sender, EventArgs e)
        {
            if (!this._loading)
            {
                this.Properties[this.PasswordProperty] = (this.passwordTextBox.Text.Length > 0) ? this.passwordTextBox.Text : null;
                this.passwordTextBox.Text = this.passwordTextBox.Text;
            }
        }

        private void SetSavePassword(object sender, EventArgs e)
        {
            
        }

        private void SetServer(object sender, EventArgs e)
        {
            if (!this._loading)
            {
                this.Properties[this.ServerProperty] = (this.serverTextBox.Text.Trim().Length > 0) ? this.serverTextBox.Text.Trim() : null;
            }
        }

        private void SetUserName(object sender, EventArgs e)
        {
            if (!this._loading)
            {
                this.Properties[this.UserNameProperty] = (this.userNameTextBox.Text.Trim().Length > 0) ? this.userNameTextBox.Text.Trim() : null;
            }
        }

        private void TrimControlText(object sender, EventArgs e)
        {
            Control control = sender as Control;
            control.Text = control.Text.Trim();
        }

        // Properties
        private string PasswordProperty
        {
            get
            {
                if (this.Properties is MySqlConnectionProperties)
                {
                    return "PWD";
                }
                return "Password";
            }
        }

        private IDataConnectionProperties Properties
        {
            get
            {
                return this._connectionProperties;
            }
        }

        private string ServerProperty
        {
            get
            {
                if (this.Properties is MySqlConnectionProperties)
                {
                    return "SERVER";
                }
                return "Data Source";
            }
        }

        private string UserNameProperty
        {
            get
            {
                if (this.Properties is MySqlConnectionProperties)
                {
                    return "UID";
                }
                return "User ID";
            }
        }
    }
}
